import { ethSafeTransfer, getExecTransactionABI, estimateGasExecTransaction } from "./ethSafeSign1_1";
import { ethers } from 'ethers';
import { global } from '@/config/config'
import { getValue } from "../deviceStorage";

let rpcProvider
let rpcNode = getValue('RPCNode')
if (rpcNode && rpcNode.includes('http')) {
    rpcProvider = new ethers.providers.JsonRpcProvider(rpcNode);
} else {
    rpcProvider = new ethers.providers.JsonRpcProvider(global.RPCNode);
}
const httpsProvider = rpcProvider

const getSignatureData = (transcationDetail) => {
    let sigs
    let signers = transcationDetail.detailedExecutionInfo?.signers || undefined
    let confirmations = transcationDetail.detailedExecutionInfo?.confirmations || undefined
    if (signers && confirmations && signers.length == 2 && confirmations.length == 2) {
        let add1 = Number(signers[0].value);
        let add2 = Number(signers[1].value);
        if (add1 < add2) {
            sigs = confirmations[0].signature + confirmations[1].signature?.substring(2)
        } else {
            sigs = confirmations[1].signature + confirmations[0].signature?.substring(2)
        }
    }
    return sigs
}

export const ethSafeSign2_2 = async (transcationDetail, mnemonicStr, customFeeData) => {
    const to = transcationDetail.txInfo?.direction === 'OUTGOING' ? transcationDetail.txInfo?.recipient?.value : transcationDetail.txInfo?.sender?.value
    const value = transcationDetail.txInfo?.transferInfo?.value
    const safeTxGas = transcationDetail.detailedExecutionInfo?.safeTxGas
    const signature = getSignatureData(transcationDetail)
    const inputData = getExecTransactionABI(to, value, safeTxGas, signature);
    const feeData = customFeeData && customFeeData.feeData ? customFeeData.feeData : await httpsProvider.getFeeData();
    const gasConsume = customFeeData && customFeeData.gasConsume ? customFeeData.gasConsume : await estimateGasExecTransaction(to, value, safeTxGas, signature);
    const txInfo = {
        toAddress: transcationDetail.safeAddress,
        feeData,
        gasConsume,
        inputData
    }
    const txHash = await ethSafeTransfer(mnemonicStr, txInfo)
    return txHash
}

export const ethSafeReject2_2 = async (transcationDetail, mnemonicStr, customFeeData) => {
    const to = transcationDetail.txInfo?.to?.value
    const value = transcationDetail.txInfo?.value
    const safeTxGas = transcationDetail.detailedExecutionInfo?.safeTxGas
    const signature = getSignatureData(transcationDetail)
    const inputData = getExecTransactionABI(to, value, safeTxGas, signature);
    const feeData = customFeeData && customFeeData.feeData ? customFeeData.feeData : await httpsProvider.getFeeData();
    const gasConsume = customFeeData && customFeeData.gasConsume ? customFeeData.gasConsume : await estimateGasExecTransaction(to, value, safeTxGas, signature);
    const txInfo = {
        toAddress: transcationDetail.safeAddress,
        feeData,
        gasConsume,
        inputData
    }
    const txHash = await ethSafeTransfer(mnemonicStr, txInfo)
    return txHash
}